import 'package:flutter/material.dart';
import 'package:flutter_ducafecat_news/common/utils/utils.dart';
import 'package:flutter_ducafecat_news/common/widgets/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_ducafecat_news/base/base_mvp_presenter.dart';
import 'package:flutter_ducafecat_news/base/base_mvp_view.dart';
import 'package:flutter_ducafecat_news/base/base_mvp_widget.dart';

class TruckEntity {
  bool isSelect = false;
  String text = "好的";
}

class SellTruckPage extends BaseWidget {
  @override
  BaseWidgetState<BasePresenter<BaseMvpView>, BaseWidget> getState() {
    // TODO: implement getState
    return _sellTruckPageState();
  }
}

class _sellTruckPageState
    extends BaseWidgetState<BasePresenter, SellTruckPage> {
  List<TruckEntity> truckTypeList = [];
  @override
  Widget buildWidget(BuildContext context) {
    // TODO: implement buildWidget
    return Container(
      child: Column(
        children: <Widget>[
          _buildGridWidget(),
          SizedBox(height:100),
          MaterialButton(
            minWidth: 600.w,
            height: 100.w,
            color: Colors.red,
            child: new Text(
              '点我',
              style: TextStyle(color: Colors.white),
            ),
            onPressed: () {},
          )
        ],
      ),
    );
  }

  Widget _buildGridWidget() {
    return Container(
        margin: EdgeInsets.only(top: 10.h),
        child: GridView.count(
            //水平子Widget之间间距
            crossAxisSpacing: 10.0,
            //垂直子Widget之间间距
            mainAxisSpacing: 30.0,
            //GridView内边距
            padding: EdgeInsets.all(10.0),
            //一行的Widget数量
            crossAxisCount: 3,
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            //子Widget宽高比例
            childAspectRatio: 2.0,
            //子Widget列表
            children: truckTypeList
                .asMap()
                .keys
                .map((index) => _buildTruckTypeItem(index))
                .toList()));
  }

  Widget _buildTruckTypeItem(item) {
    TruckEntity truckEntity = truckTypeList[item];
    return Ink(
      child: InkWell(
        child: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
              color: truckEntity.isSelect ? Colors.red : Colors.grey),
          child: Text(
            truckEntity.text,
            style: TextStyle(color: Colors.white),
          ),
        ),
        onTap: () => onItemClick(item),
      ),
    );
  }

  void onItemClick(index) {
    TruckEntity truckEntity = truckTypeList[index];
    truckEntity.isSelect = !truckEntity.isSelect;
    if (truckEntity.isSelect) {
      for (int i = 0; i < truckTypeList.length; i++) {
        truckTypeList[i].isSelect = false;
      }
      truckTypeList[index].isSelect = true;
    }
    setState(() {});
  }

  @override
  void initData() {
    // TODO: implement initData
    setAppBarText("maiche");
    setOtherHeight(getBottomNavigationBarHeight(context));
    for (int i = 0; i < 9; i++) {
      truckTypeList.add(new TruckEntity());
    }
  }

  @override
  BasePresenter<BaseMvpView> initPresenter() {
    // TODO: implement initPresenter
    return null;
  }

  @override
  void showSuccess(data) {
    // TODO: implement showSuccess
  }
}
